package com.b2b.util;

import com.alibaba.fastjson.JSONObject;
import com.b2b.model.*;
import com.b2b.model.entity.Orders;
import com.b2b.model.entity.OrdersGoods;
import com.b2b.model.entity.Service;
import com.b2b.model.entity.goods.Evaluate;
import com.b2b.model.entity.goods.Goods;
import com.b2b.model.entity.goods.Stock;
import com.b2b.service.EvaluateService;
import com.b2b.service.OrdersGoodsService;
import com.b2b.service.OrdersService;
import com.b2b.service.ServiceService;
import com.b2b.service.goodsService.GoodsService;
import com.b2b.service.goodsService.StockService;
import com.b2b.util.common.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class OrdersUtil {

    @Autowired
    OrdersGoodsService ordersGoodsService;
    @Autowired
    StockService stockService;
    @Autowired
    GoodsService goodsService;
    @Autowired
    OrdersService ordersService;
    @Autowired
    EvaluateService evaluateService;
    @Autowired
    ServiceService serviceService;

    public OrdersListDto getOrdersListDtoByOrderId(Integer orderId, OrdersListDto ordersListDto){
        OrdersGoods ordersGoods = new OrdersGoods();
        ordersGoods.setOrderId(orderId);
        List<OrdersGoods> ordersGoodsList = ordersGoodsService.select(ordersGoods);

        ordersListDto.setStockList(getStockListByOrdersGoodsList(ordersGoodsList));
        return ordersListDto;
    }

    public List<StockSend> getStockListByOrdersGoodsList(List<OrdersGoods> ordersGoodsList){
        List<StockSend> stockSendList = new ArrayList<>();
        for (OrdersGoods ordersGoods1:ordersGoodsList
        ) {
            StockSend stockSend = new StockSend();
            stockSend.setOrderId(ordersGoods1.getOrderId());
            stockSend.setStockId(ordersGoods1.getStockId());
            stockSend.setPrice(ordersGoods1.getPrice());
            stockSend.setNumber(ordersGoods1.getNumber());
            stockSendList.add(stockSend);
        }

        for (StockSend stockSend: stockSendList
        ) {
            Stock temp = stockService.selectByPrimaryKey(stockSend.getStockId());
            stockSend.setGoodsId(temp.getGoodsId());

            Goods goods = goodsService.selectByPrimaryKey(temp.getGoodsId());
            stockSend.setGoodsName(goods.getGoodsName());
            stockSend.setGoodsImgUrl(JSONObject.parseArray(goods.getGoodsImgUrlList()).getString(0));
            stockSend.setModelMap(JSONObject.parseObject(temp.getModelMap()));
        }
        return stockSendList;
    }

    public Integer getNumOfOrdersToHandle(Integer sellerId){
        int number;
        Orders orders = new Orders();
        orders.setSellerId(sellerId);
        orders.setOrderState('1');
        number = ordersService.selectCount(orders);
        orders.setOrderState('4');
        return number + ordersService.selectCount(orders);
    }
    
    public Integer getNumOfOrdersToPay(Integer buyerId){
        int number;
        Orders orders = new Orders();
        orders.setBuyerId(buyerId);
        orders.setOrderState('0');
        number = ordersService.selectCount(orders);
        orders.setOrderState('6');
        return number + ordersService.selectCount(orders);
    }

    public Integer getNumOfOrdersToDeliver(Integer buyerId){
        int number;
        Orders orders = new Orders();
        orders.setBuyerId(buyerId);
        orders.setOrderState('1');
        number = ordersService.selectCount(orders);
        orders.setOrderState('5');
        return number + ordersService.selectCount(orders);
    }

    public Integer getNumOfOrdersToEvaluate(Integer buyerId){
        int number = 0;
        Orders orders = new Orders();
        orders.setBuyerId(buyerId);
        List<Orders> ordersList = ordersService.select(orders);
        for (Orders o:ordersList
             ) {
            EvaluateDto evaluate = new EvaluateDto();
            evaluate.setOrderId(o.getOrderId());
            if(evaluateService.selectCount(evaluate) == 0)
                number++;
        }
        return number;
    }

    public Integer getNumOfOrdersToService(Integer buyerId){
        int number = 0;
        Orders orders = new Orders();
        orders.setBuyerId(buyerId);
        List<Orders> ordersList = ordersService.select(orders);
        for (Orders o:ordersList
        ) {
            ServiceDto service = new ServiceDto();
            service.setOrderId(o.getOrderId());
            service.setState('0');
            if(serviceService.selectCount(service) != 0){
                number++;
            }
        }
        return number;
    }


}
